package wnhz.com.business.shareshop.fragment;

import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import wnhz.com.business.CompleteInfoActivity;
import wnhz.com.business.MyApplication;
import wnhz.com.business.R;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.base.MyBaseFragment;
import wnhz.com.business.groupshop.bean.UserInfoBean;
import wnhz.com.business.groupshop.ui.CreditNumActivity;
import wnhz.com.business.groupshop.ui.HelpCenterActivity;
import wnhz.com.business.groupshop.ui.MessageActivity;

import wnhz.com.business.rebateshop.activity.mine.SettingActivity;
import wnhz.com.business.shareshop.activity.mine.AllOrderActivity;
import wnhz.com.business.shareshop.activity.mine.DepositActivity;
import wnhz.com.business.shareshop.activity.mine.RebateActivity;
import wnhz.com.business.shareshop.activity.mine.ShareActivity;
import wnhz.com.business.shareshop.ui.FundActivityS;
import wnhz.com.business.shareshop.ui.MakeMerchatActivityS;
import wnhz.com.business.shareshop.ui.MyAttentionActivityS;
import wnhz.com.business.shareshop.ui.MyCollectActivityS;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.CircleImageView;

/**
 * Created by Administrator on 2017-12-13 0013.
 */

public class MyFragment extends MyBaseFragment {

    @BindView(R.id.iv_r_head)  //头像
            CircleImageView ivRHead;
    @BindView(R.id.tv_r_userName)//名字
            TextView tvRUserName;
    @BindView(R.id.tv_r_phone)//电话
            TextView tvRPhone;
    @BindView(R.id.ll_r_xingyongnum)//信用值
            LinearLayout llRXingyongnum;
    @BindView(R.id.ll_r_message)//消息
            LinearLayout llRMessage;
    @BindView(R.id.ll_r_allOrder)//全部订单
            LinearLayout llRAllOrder;
    @BindView(R.id.ll_r_one)//待付款
            LinearLayout llROne;
    @BindView(R.id.ll_r_two)//待发货
            LinearLayout llRTwo;
    @BindView(R.id.ll_r_three)//待收货
            LinearLayout llRThree;
    @BindView(R.id.ll_r_four)//待评价
            LinearLayout llRFour;
    @BindView(R.id.ll_r_fifth)//退款/售后
            LinearLayout llRFifth;
    @BindView(R.id.ll_r_collect)//我的收藏
            LinearLayout llRCollect;
    @BindView(R.id.ll_r_attention)//我的关注
            LinearLayout llRAttention;
    @BindView(R.id.ll_r_fund)//我的钱包
            LinearLayout llRFund;
    @BindView(R.id.ll_r_deposit)//我的押金
            LinearLayout llRDeposit;
    @BindView(R.id.ll_r_rebate)//我的返利
            LinearLayout llRRebate;
    @BindView(R.id.ll_r_share)//我的分享
            LinearLayout llRShare;
    @BindView(R.id.ll_r_userFaith)//我的信用值
            LinearLayout llRUserFaith;
    @BindView(R.id.ll_r_shenqin)//申请成为共享商家
            LinearLayout llRShenqin;
    @BindView(R.id.ll_r_help)//帮助中心
            LinearLayout llRHelp;
    @BindView(R.id.ll_r_setting)//设置
            LinearLayout llRSetting;
    @BindView(R.id.credibility_value)
    TextView credibility_value;//信誉值

    private MyBaseActivity activity;
    Unbinder unbinder;

    UserInfoBean userInfoBean;

    @Override
    protected void handleMsg(Message msg) {
        switch (msg.what) {
            case 5001:
                break;
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_share_my, container, false);
        activity = (MyBaseActivity) getActivity();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        getInfoData();
        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);

        return rootView;
    }

    //获取用户信息
    private void getInfoData(){
        Map<String,Object> maps = new HashMap<>();
        maps.put("token",MyApplication.getToken());
        XUtil.Post(Url.UCENTER_USERINFO,maps,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if ("1".equals(jsonObject.getString("re"))) {
                        Gson gson = new Gson();
                        userInfoBean = gson.fromJson(result,UserInfoBean.class);
                        if (null != userInfoBean) {
                            tvRUserName.setText(userInfoBean.getInfo().getUsername());
                            tvRPhone.setText(userInfoBean.getInfo().getMobile());
                            Glide.with(getActivity()).load(userInfoBean.getInfo().getHead_img()).dontAnimate().into(ivRHead);
                            credibility_value.setText(userInfoBean.getInfo().getCredit());

                        }
                    }else {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void loadInfo() {
        handler.sendEmptyMessage(5001);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.iv_r_head, R.id.tv_r_userName, R.id.tv_r_phone, R.id.ll_r_xingyongnum, R.id.ll_r_message, R.id.ll_r_allOrder, R.id.ll_r_one, R.id.ll_r_two, R.id.ll_r_three, R.id.ll_r_four, R.id.ll_r_fifth, R.id.ll_r_collect, R.id.ll_r_attention, R.id.ll_r_fund, R.id.ll_r_deposit, R.id.ll_r_rebate, R.id.ll_r_share, R.id.ll_r_userFaith, R.id.ll_r_shenqin, R.id.ll_r_help, R.id.ll_r_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_r_head://修改个人信息 -共享商城
                startActivity(CompleteInfoActivity.createIntent(activity, "2"));
                break;
            case R.id.tv_r_userName://个人信息名字
                startActivity(CompleteInfoActivity.createIntent(activity, "2"));
                break;
            case R.id.tv_r_phone://个人信息电话
                startActivity(CompleteInfoActivity.createIntent(activity, "2"));
                break;
            case R.id.ll_r_xingyongnum://信用值
                startActivity(CreditNumActivity.createIntent(activity));
                break;
            case R.id.ll_r_message://消息
                startActivity(MessageActivity.createIntent(activity));
                break;
            case R.id.ll_r_allOrder://所有订单
                startActivity(AllOrderActivity.createIntent(activity, "0"));
                break;
            case R.id.ll_r_one://待付款
                startActivity(AllOrderActivity.createIntent(activity, "1"));
                break;
            case R.id.ll_r_two://待发货
                startActivity(AllOrderActivity.createIntent(activity, "2"));
                break;
            case R.id.ll_r_three://待收货
                startActivity(AllOrderActivity.createIntent(activity, "3"));
                break;
            case R.id.ll_r_four://待评价
                startActivity(AllOrderActivity.createIntent(activity, "4"));
                break;
            case R.id.ll_r_fifth://退款、售后
                startActivity(AllOrderActivity.createIntent(activity, "5"));
                break;
            case R.id.ll_r_collect://我的收藏
                startActivity(MyCollectActivityS.createIntent(activity));
                break;
            case R.id.ll_r_attention://我的关注
                startActivity(MyAttentionActivityS.createIntent(activity));
                break;
            case R.id.ll_r_fund://我的钱包
                startActivity(FundActivityS.createIntent(activity));
                break;

            case R.id.ll_r_deposit://我的押金
                startActivity(DepositActivity.createIntent(activity));
                break;
            case R.id.ll_r_rebate://我的返利
                startActivity(RebateActivity.createIntent(activity));
                break;
            case R.id.ll_r_share://我的分享
                startActivity(ShareActivity.createIntent(activity));
                break;

            case R.id.ll_r_userFaith://我的信用值
                startActivity(CreditNumActivity.createIntent(activity));
                break;
            case R.id.ll_r_shenqin://申请成为共享商家
                startActivity(MakeMerchatActivityS.createIntent(activity));
                break;
            case R.id.ll_r_help://帮助中心
                startActivity(HelpCenterActivity.createIntent(activity, 2)); //int   type   1-规则  2-帮助中心  3-信用秘籍
                break;
            case R.id.ll_r_setting://设置
                startActivity(SettingActivity.createIntent(activity));
                break;
        }
    }
}